package com.yanqu.road.dao.impl.activity.deepfish;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.deepfishing.config.DeepFishingBeachCombConfig;
import com.yanqu.road.entity.activity.deepfishing.config.DeepFishingEquipConfig;
import com.yanqu.road.entity.activity.deepfishing.config.DeepFishingEventConfig;
import com.yanqu.road.entity.activity.deepfishing.config.DeepFishingFishConfig;
import com.yanqu.road.entity.activity.deepfishing.entity.DeepFishingOcurConfig;
import com.yanqu.road.entity.config.system.PropertyWeightItem;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DeepFishConfigDaoImpl extends TempDao {
    private static String getDeepFishingFishConfigSql = "select * from `t_s_activity_seafishing_object` where `activityId`=?";

    public Map<Integer,DeepFishingFishConfig> getDeepFishingFishConfigList(int activityId) {
        Map<Integer,DeepFishingFishConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDeepFishingFishConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DeepFishingFishConfig deepFishingFishConfig = new DeepFishingFishConfig();
                deepFishingFishConfig.setActivityId(rs.getInt("activityId"));
                deepFishingFishConfig.setObjectId(rs.getInt("objectId"));
                deepFishingFishConfig.setType(rs.getInt("type"));
                deepFishingFishConfig.setLittleType(rs.getInt("littleType"));
                deepFishingFishConfig.setQuality(rs.getInt("quality"));
                String ocurConfigString = rs.getString("ocurParam");
                //出没配置
                List<DeepFishingOcurConfig> ocurConfigList = new ArrayList<>();
                List<String> stringList = StringUtils.stringToStringList(ocurConfigString, "\\|");
                for (String string : stringList) {
                    List<Integer> integerList = StringUtils.stringToIntegerList(string, ";");
                    if (integerList.size() != 2) {
                        if (integerList.size() == 1 && integerList.get(0) == 0){
                            DeepFishingOcurConfig ocurConfig = new DeepFishingOcurConfig(0, 0);
                            ocurConfigList.add(ocurConfig);
                            continue;
                        }
                        logger.error("活动id{}t_s_activity_seafishing_object表ocurParam的{}配置有误", activityId, deepFishingFishConfig.getObjectId());
                        continue;
                    }
                    DeepFishingOcurConfig ocurConfig = new DeepFishingOcurConfig(integerList.get(0), integerList.get(1));
                    ocurConfigList.add(ocurConfig);
                }
                ocurConfigList.sort(Comparator.comparing(DeepFishingOcurConfig::getOcurMinDeep));
                deepFishingFishConfig.setOcurConfigList(ocurConfigList);
                deepFishingFishConfig.setSpeed(rs.getInt("speed"));
                //金币范围
                String coinRange = rs.getString("coinRange");
                List<Integer> coinRangeList = StringUtils.stringToIntegerList(coinRange, "\\|");
                if (coinRangeList.size() != 2) {
                    logger.error("活动id{}t_s_activity_seafishing_object表coinRange的{}配置有误", activityId, deepFishingFishConfig.getObjectId());
                    continue;
                }
                deepFishingFishConfig.setCoinRange(coinRangeList);

                deepFishingFishConfig.setFollowPro(rs.getInt("followPro"));
                deepFishingFishConfig.setFollowRange(rs.getInt("followRange"));
                deepFishingFishConfig.setFollowSpeed(rs.getInt("followSpeed"));
                deepFishingFishConfig.setName(rs.getString("name"));
                deepFishingFishConfig.setScale(rs.getInt("scale"));
                //重量范围
                String weightRange = rs.getString("weightRange");
                List<Integer> weightRangeList = StringUtils.stringToIntegerList(weightRange, "\\|");
                if (weightRangeList.size() != 2) {
                    logger.error("活动id{}t_s_activity_seafishing_object表weightRange的{}配置有误", activityId, deepFishingFishConfig.getObjectId());
                    continue;
                }
                deepFishingFishConfig.setWeightRange(weightRangeList);
                deepFishingFishConfig.setDesc(rs.getString("desc"));
                deepFishingFishConfig.setMoveParam(rs.getString("moveParam"));
                deepFishingFishConfig.setPoolType(rs.getInt("poolType"));

                map.put(deepFishingFishConfig.getObjectId(),deepFishingFishConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getDeepFishingEquipConfigSql = "select * from `t_s_activity_seafishing_equip` where `activityId`=?";
    public Map<Integer ,DeepFishingEquipConfig> getDeepFishingEquipConfigList(int activityId) {
        Map<Integer ,DeepFishingEquipConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDeepFishingEquipConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DeepFishingEquipConfig deepFishingEquipConfig = new DeepFishingEquipConfig();
                deepFishingEquipConfig.setActivityId(rs.getInt("activityId"));
                deepFishingEquipConfig.setLvId(rs.getInt("lvId"));
                deepFishingEquipConfig.setType(rs.getInt("type"));
                deepFishingEquipConfig.setLv(rs.getInt("lv"));
                deepFishingEquipConfig.setCost(rs.getInt("cost"));
                //解析效果值
                String effectValue = rs.getString("effectValue");
                List<Integer> effectList = StringUtils.stringToIntegerList(effectValue, "\\|");
//                if ((deepFishingEquipConfig.getType() == 1 && effectList.size() != 3) || (deepFishingEquipConfig.getType() != 1 && effectList.size() != 2)) {
//                    logger.error("活动id{}t_s_activity_seafishing_equip表effectValue的{}配置有误", activityId, deepFishingEquipConfig.getLvId());
//                    continue;
//                }
                deepFishingEquipConfig.setEffectValue(effectList);
                map.put(deepFishingEquipConfig.getLvId(),deepFishingEquipConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getDeepFishingEventConfigSql = "select * from `t_s_activity_seafishing_event` where `activityId`=?";
    public Map<Integer,DeepFishingEventConfig> getDeepFishingEventConfigList(int activityId) {
        Map<Integer,DeepFishingEventConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDeepFishingEventConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DeepFishingEventConfig deepFishingEventConfig = new DeepFishingEventConfig();
                deepFishingEventConfig.setActivityId(rs.getInt("activityId"));
                deepFishingEventConfig.setEventId(rs.getInt("eventId"));
                deepFishingEventConfig.setType(rs.getInt("type"));
                deepFishingEventConfig.setLittle(rs.getInt("little"));
                deepFishingEventConfig.setWeight(rs.getInt("weight"));
                deepFishingEventConfig.setEventValue(rs.getString("eventValue"));

                String ocurConfigString = rs.getString("ocurParam");
                //出没配置
                List<DeepFishingOcurConfig> ocurConfigList = new ArrayList<>();
                List<String> stringList = StringUtils.stringToStringList(ocurConfigString, "\\|");
                for (String string : stringList) {
                    List<Integer> integerList = StringUtils.stringToIntegerList(string, ";");
                    if (integerList.size() != 2) {
                        if (integerList.size() == 1 && integerList.get(0) == 0){
                            DeepFishingOcurConfig ocurConfig = new DeepFishingOcurConfig(0, 0);
                            ocurConfigList.add(ocurConfig);
                            continue;
                        }
                        logger.error("活动id{}t_s_activity_seafishing_event表ocurParam的{}配置有误", activityId, deepFishingEventConfig.getEventId());
                        continue;
                    }
                    DeepFishingOcurConfig ocurConfig = new DeepFishingOcurConfig(integerList.get(0), integerList.get(1));
                    ocurConfigList.add(ocurConfig);
                }
                deepFishingEventConfig.setOcurConfigList(ocurConfigList);
                map.put(deepFishingEventConfig.getEventId(),deepFishingEventConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getDeepFishingBeachCombConfigSql = "select * from `t_s_activity_seafishing_chasing` where `activityId`=?";
    public  Map<Integer,List<DeepFishingBeachCombConfig>>  getDeepFishingBeachCombConfigList(int activityId) {
        Map<Integer,List<DeepFishingBeachCombConfig>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getDeepFishingBeachCombConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                DeepFishingBeachCombConfig deepFishingBeachCombConfig = new DeepFishingBeachCombConfig();
                deepFishingBeachCombConfig.setActivityId(rs.getInt("activityId"));
                deepFishingBeachCombConfig.setPointId(rs.getInt("pointId"));
                deepFishingBeachCombConfig.setScene(rs.getInt("scene"));
                deepFishingBeachCombConfig.setPointCode(rs.getInt("pointCode"));
                deepFishingBeachCombConfig.setPos(rs.getString("pos"));

                List<PropertyWeightItem> normalWeightItems = new ArrayList<>();
                String weightString = rs.getString("weight");
                if (!weightString.equals("0")){
                    List<String> itemString = StringUtils.stringToStringList(weightString, "\\|");
                    for (String string : itemString){
                        List<Integer> itemAndWeight = StringUtils.stringToIntegerList(string,";");
                        PropertyWeightItem propertyWeightItem = new PropertyWeightItem(new Property(itemAndWeight.get(0),1),itemAndWeight.get(1));
                        normalWeightItems.add(propertyWeightItem);
                    }
                }

                deepFishingBeachCombConfig.setWeight(normalWeightItems);

                deepFishingBeachCombConfig.setType(rs.getInt("type"));

                List<PropertyWeightItem> specialWeightItems = new ArrayList<>();
                String specialWeightString = rs.getString("bigReward");
                if (!specialWeightString.equals("0")){
                    List<String> specialItemString = StringUtils.stringToStringList(specialWeightString, "\\|");
                    for (String string : specialItemString){
                        List<Integer> itemAndWeight = StringUtils.stringToIntegerList(string,";");
                        PropertyWeightItem propertyWeightItem = new PropertyWeightItem(new Property(itemAndWeight.get(0),1),itemAndWeight.get(1));
                        specialWeightItems.add(propertyWeightItem);
                    }
                }
                deepFishingBeachCombConfig.setBigReward(specialWeightItems);
                deepFishingBeachCombConfig.setUrl(rs.getInt("url"));
                map.computeIfAbsent(deepFishingBeachCombConfig.getScene(), k -> new ArrayList<>());
                map.get(deepFishingBeachCombConfig.getScene()).add(deepFishingBeachCombConfig);

            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }



}
